<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring依赖注入完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.2rem;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 1rem;
            flex-shrink: 0;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #ffd89b 0%, #19547b 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                Spring 依赖注入
                <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">完全指南</span>
            </h1>
            <p class="text-xl md:text-2xl font-light max-w-3xl mx-auto leading-relaxed opacity-95">
                掌握 Spring 框架的核心机制，实现优雅的对象解耦与灵活架构
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-plug text-4xl mb-2"></i>
                    <p class="text-sm">解耦合</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-cogs text-4xl mb-2"></i>
                    <p class="text-sm">灵活性</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync text-4xl mb-2"></i>
                    <p class="text-sm">可维护</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-10 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>Spring框架中，依赖注入（Dependency Injection，DI）是一种实现对象之间依赖关系的方式。它通过将对象的依赖关系交由容器管理，从而实现对象之间的解耦和灵活性。Spring框架提供了多种方式来实现依赖注入，包括构造函数注入、Setter方法注入、字段注入和注解注入。
            </p>
        </div>

        <!-- Dependency Injection Visualization -->
        <div class="mb-16">
            <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">依赖注入方式全览</h2>
            <div class="mermaid">
                graph TD
                    A[Spring 依赖注入] --> B[构造函数注入]
                    A --> C[Setter方法注入]
                    A --> D[字段注入]
                    A --> E[注解注入]
                    A --> F[类型注入]
                    A --> G[名字注入]
                    
                    B --> B1[XML配置]
                    B --> B2[@Autowired]
                    
                    C --> C1[property标签]
                    C --> C2[@Autowired]
                    
                    D --> D1[@Autowired]
                    D --> D2[@Resource]
                    
                    E --> E1[@Autowired]
                    E --> E2[@Resource]
                    E --> E3[@Inject]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#ffd89b,stroke:#333,stroke-width:2px
                    style C fill:#ffd89b,stroke:#333,stroke-width:2px
                    style D fill:#ffd89b,stroke:#333,stroke-width:2px
                    style E fill:#ffd89b,stroke:#333,stroke-width:2px
                    style F fill:#ffd89b,stroke:#333,stroke-width:2px
                    style G fill:#ffd89b,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Injection Methods -->
        <div class="grid gap-8">
            <!-- Constructor Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">1</span>
                    <h3 class="text-2xl font-bold text-gray-800">构造函数注入</h3>
                </div>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    构造函数注入是通过对象的构造函数来注入依赖关系。在Spring框架中，可以通过配置文件或注解来指定构造函数参数的值，从而在创建对象时自动注入依赖。例如，使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">&lt;constructor-arg&gt;</code> 标签在XML配置文件中配置构造函数参数，或者使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Autowired</code> 注解在构造函数上标记依赖关系。
                </p>
                <div class="code-block p-6">
                    <pre class="text-sm"><code>public class A {
    // A类的实现
}

public class B {
    // B类的实现
}

public class C {
    private A a;
    private B b;
    
    // 通过构造器注入依赖
    public C(A a, B b) {
        this.a = a;
        this.b = b;
    }
    
    // 使用示例
    C c = new C(new A(), new B());
}</code></pre>
                </div>
            </div>

            <!-- Setter Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">2</span>
                    <h3 class="text-2xl font-bold text-gray-800">Setter方法注入</h3>
                </div>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    Setter方法注入是通过对象的Setter方法来注入依赖关系。在Spring框架中，可以通过配置文件或注解来指定Setter方法的参数值，容器在创建对象后会调用相应的Setter方法来设置依赖。例如，使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">&lt;property&gt;</code> 标签在XML配置文件中配置Setter方法的参数，或者使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Autowired</code> 注解在Setter方法上标记依赖关系。
                </p>
                <div class="code-block p-6">
                    <pre class="text-sm"><code>public class A {
    // A类的实现
}

public class B {
    // B类的实现
}

public class C {
    private A a;
    private B b;
    
    // Setter方法
    public void setA(A a) {
        this.a = a;
    }
    
    public void setB(B b) {
        this.b = b;
    }
    
    // 使用示例
    C c = new C();
    c.setA(new A());
    c.setB(new B());
}</code></pre>
                </div>
            </div>

            <!-- Field Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">3</span>
                    <h3 class="text-2xl font-bold text-gray-800">字段注入</h3>
                </div>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    字段注入是通过直接注入对象的字段来实现依赖注入。在Spring框架中，可以使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Autowired</code> 注解或 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Resource</code> 注解在字段上标记依赖关系。容器在创建对象后，会自动将相应的依赖对象注入到字段中。
                </p>
            </div>

            <!-- Annotation Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">4</span>
                    <h3 class="text-2xl font-bold text-gray-800">注解注入</h3>
                </div>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    注解注入是通过使用特定的注解来标记依赖关系，从而实现依赖注入。Spring框架提供了多个注解来支持注解注入，包括 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Autowired</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Resource</code>、<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Inject</code> 等。开发人员可以在类、构造函数、字段或Setter方法上使用这些注解来标记依赖关系。
                </p>
            </div>

            <!-- Type Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-10 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">5</span>
                    <h3 class="text-2xl font-bold text-gray-800">类型注入</h3>
                </div>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    类型注入是根据类型自动匹配并注入相应的Bean。需要注意的是，如果一个接口有多个实现类，可能会导致注入失败。
                </p>
                <div class="code-block p-6">
                    <pre class="text-sm"><code>@RestController
public class EmpController {
    
    // 注入EmpService的类型
    // EmpService是一个接口，可能有多个实现类
    // 如果存在多个实现